package com.studykun.client.ui;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.studykun.client.model.Card;
import com.studykun.client.model.Deck;
import com.studykun.client.model.Syllabus;

public class CardListDialog2 extends DialogBox {
  final Syllabus syllabus_;
  final FlowPanel clientPanel_ = new FlowPanel();
  final FlowPanel toolPanel_ = new FlowPanel();
  final ArrayList<CheckBox> checkBoxes_ = new ArrayList<CheckBox>();
  
  int lastCheckBoxIndex_ = -1;
  
  public CardListDialog2(
      final Syllabus syllabus) {
    syllabus_ = syllabus;
    
    this.setText("Select cards");
    this.setAnimationEnabled(true);
    final Button closeButton = new Button("Close");
    
    final Anchor selectAllAnchor = new Anchor("all");
    selectAllAnchor.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        for (final CheckBox checkBox : checkBoxes_) {
          final boolean previousValue = checkBox.getValue();
          checkBox.setValue(true);
          ValueChangeEvent.fireIfNotEqual(
              checkBox, previousValue, checkBox.getValue());
        }
      }
    });
    
    final Anchor unselectAllAnchor = new Anchor("none");
    unselectAllAnchor.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        for (final CheckBox checkBox : checkBoxes_) {
          final boolean previousValue = checkBox.getValue();
          checkBox.setValue(false);
          ValueChangeEvent.fireIfNotEqual(
              checkBox, previousValue, checkBox.getValue());
        }
      }
    });
    
    toolPanel_.add(new InlineLabel("Select: "));
    toolPanel_.add(selectAllAnchor);
    toolPanel_.add(new InlineLabel(", "));
    toolPanel_.add(unselectAllAnchor);
    
    clientPanel_.setStyleName("cardList");
    toolPanel_.getElement().getStyle().setProperty("textAlign", "left");

    // We can set the id of a widget by accessing its Element
    closeButton.getElement().setId("closeButton");
    VerticalPanel dialogVPanel = new VerticalPanel();
    dialogVPanel.addStyleName("dialogVPanel");
    dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
    dialogVPanel.add(toolPanel_);
    dialogVPanel.add(clientPanel_);
    dialogVPanel.add(closeButton);
    this.setWidget(dialogVPanel);

    // Add a handler to close the DialogBox
    closeButton.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        CardListDialog2.this.hide();
      }
    });
  }
  
  public interface Row {
    public boolean isHeading();
    
    public String getText();
  }
  
  @Override
  public void show() {
    clientPanel_.clear();
    final CellTable<Row> table = new CellTable<Row>();
    table.getElement().setClassName("cardGrid");
    
    // Create a data provider.
    ListDataProvider<Row> dataProvider = new ListDataProvider<Row>();

    // Connect the table to the data provider.
    dataProvider.addDataDisplay(table);
    
    final TextColumn<Row> textColumn = new TextColumn<Row>() {
      @Override
      public String getValue(final Row row) {
        return row.getText();
      }
    };
    table.addColumn(textColumn);
    final List<Row> list = dataProvider.getList();
    
    checkBoxes_.clear();
    
    for (final Deck deck : syllabus_.decks()) {
      final InlineLabel deckRowHeading = new InlineLabel(deck.name());
      deckRowHeading.setStylePrimaryName("deckRowHeading");
      
      list.add(new Row() {
        @Override
        public boolean isHeading() {
          return true;
        }

        @Override
        public String getText() {
          return deck.name();
        }
      });
      
      for (final Card card : deck.cards()) {
        list.add(new Row() {
          @Override
          public boolean isHeading() {
            return false;
          }

          @Override
          public String getText() {
            return card.kanjiClean();
          }
        });
      }
    }
    clientPanel_.add(table);
    super.show();
  }
}
